//
// Created by root on 2020/4/4.
//

#ifndef HEATBEATSERVER_PACKHANDLE_H
#define HEATBEATSERVER_PACKHANDLE_H

#include <functional>
#include <unordered_map>
#include <string>

#include "muduo/net/TcpServer.h"
#include "nlohmann/json.hpp"

class CPackHandle {
public:
    using TcpConnectionWeakPtr = std::weak_ptr<muduo::net::TcpConnection>;

public:
    CPackHandle();
    ~CPackHandle();

    void HandleCommand(TcpConnectionWeakPtr _pConn, int _iProtocolNo, const std::string &_csMessageBody);

private:
    /*
     * 处理在线用户
     * @param _jsonMsg
     */
    void _HandleOnlineUser(TcpConnectionWeakPtr _pConn, nlohmann::json &_jsonMsg);
    /**
     * 创建聊天室
     * @param _pConn
     * @param _jsonMsg
     */
    void _HandleCreateChat(TcpConnectionWeakPtr _pConn, nlohmann::json &_jsonMsg);
    /**
     * @param _pConn
     * @param _jsonMsg
     */
    void _HandleRemoveChat(TcpConnectionWeakPtr _pConn, nlohmann::json &_jsonMsg);

    /**
     * 转发文本信息
     * @param _pConn
     * @param _jsonMsg
     */
    void _HandleForwardMsg(TcpConnectionWeakPtr _pConn, nlohmann::json &_jsonMsg);
    /**
     * 终止连接
     * @param _pConn
     * @param _jsonMsg
     */
    void _HandleConnTerminal(TcpConnectionWeakPtr _pConn, nlohmann::json &_jsonMsg);
    /**
     * 登陆请求
     * @param _pConn
     * @param _jsonMsg
     */
    void _HandleLogin(TcpConnectionWeakPtr _pConn, nlohmann::json &_jsonMsg);
    /**
     * 心跳包
     * @param _pConn
     * @param _jsonMsg
     */
    void _HeartbeatPack(TcpConnectionWeakPtr _pConn, nlohmann::json &_jsonMsg);
    /**
     * 退出
     * @param _pConn
     * @param _jsonMsg
     */
    void _HandleQuit(TcpConnectionWeakPtr _pConn, nlohmann::json &_jsonMsg);

private:
    using CallBackFunType = std::function<void(TcpConnectionWeakPtr _pConn, nlohmann::json &)>;
    std::unordered_map<int, CallBackFunType> m_hashCommand;    ///< 协议——处理函数映射表
};


#endif //HEATBEATSERVER_PACKHANDLE_H
